package com.sunthy.mall.controller.client;

import com.google.gson.Gson;
import com.sunthy.mall.model.admin.Result;
import com.sunthy.mall.model.admin.vo.goods.AllTypeVo;
import com.sunthy.mall.service.admin.goods.GoodsService;
import com.sunthy.mall.service.admin.goods.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/mall/index/*")
public class IndexServlet extends HttpServlet {

    private Gson gson = new Gson();
    private GoodsService goodsService = new GoodsServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if ("/getType".equals(pathInfo)) {
            getType(request, response);
        }
    }

    /**
     * 获取类目的逻辑:
     * 1.查询类目
     * 2.返回结果
     *
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 查询数据库
        List<AllTypeVo> allTypeVoList = goodsService.allTypes();
        // 返回结果
        response.getWriter().println(gson.toJson(Result.ok(allTypeVoList)));
    }
}
